package by.bsu.scs.db.dao;

// Generated 05.03.2011 11:02:55 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import by.bsu.scs.model.Message;
import by.bsu.scs.model.User;
import by.bsu.scs.util.db.HibernateUtil;

/**
 * Home object for domain model class Message.
 * 
 * @see by.bsu.scs.model.Message
 * @author Hibernate Tools
 */
public class MessageHome {

	private static final String GET_MESSAGE_BY_ID_USER_TO = "getMessageByIdUserTo";
	private static final String GET_MESSAGE_BY_ID_USER_FROM = "getMessageByIdUserFrom";
	private static final String GET_NOT_READ_MESSAGES_BY_ID_USER_TO = "getNotReadMessagesByIdUserTo";
	private static final String USER_ID = "userId";
	private static final String IS_READ = "isRead";
	private static final String IS_VISIBLE_RESEIVER = "isVisibleReceiver";
	private static final String IS_VISIBLE_SENDER = "isVisibleSender";
	private static final Logger LOGGER = Logger.getLogger(MessageHome.class);

	private Session session = null;
	private Transaction transaction = null;

	public void persist(Message transientInstance) {
		LOGGER.info("persisting Message instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			LOGGER.info("persist successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("persist failed", e.getCause());
		}
	}

	public void attachDirty(Message instance) {
		LOGGER.info("attaching dirty Message instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.saveOrUpdate(instance);
			transaction.commit();
			LOGGER.info("attach successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("attach failed", e.getCause());
		}
	}

	@SuppressWarnings("deprecation")
	public void attachClean(Message instance) {
		LOGGER.info("attaching clean Message instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			transaction.commit();
			LOGGER.info("attach successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("attach failed", e.getCause());
		}
	}

	public void delete(Message persistentInstance) {
		LOGGER.info("deleting Message instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			session.delete(persistentInstance);
			transaction.commit();
			LOGGER.info("delete successful");
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("delete failed", e.getCause());
		}
	}

	public Message merge(Message detachedInstance) {
		LOGGER.info("merging Message instance");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			Message result = (Message) session.merge(detachedInstance);
			transaction.commit();
			LOGGER.info("merge successful");
			return result;
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error("merge failed", e.getCause());
			return null;
		}
	}

	public Message findById(int id) {
		LOGGER.info("getting Message instance with id: " + id);
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			Message instance = (Message) session.get(
					"by.bsu.scs.model.Message", id);
			if (instance == null) {
				LOGGER.info("get successful, no instance found");
			} else {
				LOGGER.info("get successful, instance found");
			}
			transaction.commit();
			return instance;
		} catch (RuntimeException e) {
			transaction.rollback();
			LOGGER.error("get failed", e.getCause());
			return null;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByExample(Message instance) {
		LOGGER.info("finding Message instance by example");
		session = HibernateUtil.openCurrentSession();
		try {
			transaction = session.beginTransaction();
			Example example = Example.create(instance).excludeZeroes();
			List results = session.createCriteria("by.bsu.scs.model.Message")
					.add(example).list();
			LOGGER.info("find by example successful, result size: "
					+ results.size());
			transaction.commit();
			return results;
		} catch (RuntimeException e) {
			transaction.rollback();
			LOGGER.error("find by example failed", e.getCause());
			return null;
		}
	}

	// ***************** Manually created methods ******************

	@SuppressWarnings("unchecked")
	public List<Message> getMessageByIdUserTo(User user) {
		LOGGER.info("finding Messages by id user to");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<Message> messageList = null;
		try {
			transaction = session.beginTransaction();
			messageList = session.getNamedQuery(GET_MESSAGE_BY_ID_USER_TO)
					.setInteger(USER_ID, user.getUserId())
					.setBoolean(IS_VISIBLE_RESEIVER, true).list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return messageList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Message> getMessageByIdUserFrom(User user) {
		LOGGER.info("finding Messages by id user from");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<Message> messageList = null;
		try {
			transaction = session.beginTransaction();
			messageList = session.getNamedQuery(GET_MESSAGE_BY_ID_USER_FROM)
					.setInteger(USER_ID, user.getUserId())
					.setBoolean(IS_VISIBLE_SENDER, true).list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return messageList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Message> getNotReadMessagesByIdUserTo(User user) {
		LOGGER.info("finding not read Messages by id user to");
		Session session = HibernateUtil.openCurrentSession();
		Transaction transaction = null;
		List<Message> messageList = null;
		try {
			transaction = session.beginTransaction();
			messageList = session.getNamedQuery(GET_NOT_READ_MESSAGES_BY_ID_USER_TO)
					.setInteger(USER_ID, user.getUserId())
					.setBoolean(IS_READ, false).list();
			transaction.commit();
			LOGGER.info("find successful");
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			LOGGER.error("find failed", e.getCause());
		}
		return messageList;
	}

}
